//
//  DCProgressHUDTool.m
//  Driver
//
//  Created by 大橙子 on 2018/11/7.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCProgressHUDTool.h"
#import "DCProgressTableViewCell.h"
#import "DCChooseTableVIew.h"
#import "SingleChooseCollectview.h"
@interface DCProgressHUDTool ()
@property(nonatomic,copy)SureBtnBlock sureBlock;
@property(nonatomic,copy)CancleBtnBlock cancleBlock;
@property (nonatomic,assign) NSInteger type;
@property (nonatomic,strong) UITextField *inputTextfield;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;

@property (nonatomic,strong) NSMutableArray *chooseArray;
@property (nonatomic,copy) NSString *contentText;

@property (nonatomic,strong) DCImageAndTextField *plateNumLabel;
@property (nonatomic,strong) DCImageAndTextField *driverNameLabel;

@end
@implementation DCProgressHUDTool
+(instancetype)sharedHUDTool
{
    static DCProgressHUDTool *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[DCProgressHUDTool alloc]init];
    });
    return _manager;
}
-(UIView *)showProgressWithType:(NSInteger)type datasource:(NSMutableArray *)dataSource title:(NSString *)title context:(NSString *)context sureBlock:(SureBtnBlock)sureBlock cancleBlock:(CancleBtnBlock)cancleBlock
{
    __weak typeof(self) weakself = self;
    self.sureBlock = sureBlock;
    self.cancleBlock = cancleBlock;
    self.dataSource = dataSource;
    self.type = type;
    
    CGFloat kBaseHeight = 40;
    CGFloat tableViewHeight = 0.0;
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];
    backView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    backView.userInteractionEnabled = YES;
    
    UIView *contentView = [[UIView alloc]init];
    contentView.width = 0.8*backView.width;
    if (type == 0) {
        tableViewHeight = dataSource.count > 5 ?180:(dataSource.count+1)*30;
        contentView.height =tableViewHeight+kBaseHeight*4+20;
    }else if (type == 1){
        int a = dataSource.count%3;
        tableViewHeight = dataSource.count > 12 ?200:(a == 0?dataSource.count/3:dataSource.count/3+1)*50;
        contentView.height =tableViewHeight+kBaseHeight*3+20;
    }else if (type == 2){
        tableViewHeight = 50*2+10;
        contentView.height =tableViewHeight+kBaseHeight*3+20;
    }
    contentView.center = CGPointMake(backView.centerX, backView.centerY);
    contentView.userInteractionEnabled = YES;
    contentView.layer.cornerRadius = 10;
    [backView addSubview:contentView];
    
    UILabel *titlelLabel = [[UILabel alloc]init];
    titlelLabel.text = title;
    titlelLabel.textAlignment = NSTextAlignmentCenter;
    titlelLabel.font = [UIFont systemFontOfSize:20];
    titlelLabel.textColor = [UIColor blackColor];
    titlelLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titlelLabel];
    [titlelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.left.equalTo(contentView);
        make.right.equalTo(contentView);
        make.height.mas_offset(kBaseHeight);
    }];
    
    CGFloat line = 0.5;
    UILabel *line1 = [[UILabel alloc]init];
    line1.backgroundColor = [UIColor lightGrayColor];
    line1.alpha = 0.8;
    [contentView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titlelLabel.mas_bottom);
        make.left.equalTo(contentView);
        make.right.equalTo(contentView);
        make.height.mas_offset(line);
    }];
    
    DCImageAndTextLabel *textLabel = [[DCImageAndTextLabel alloc]init];
    textLabel.textLabel.font = FONT(17);
    textLabel.textLabel.textColor = [UIColor blackColor];
    textLabel.textLabel.text = context;
    textLabel.textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.imageView.image = [UIImage imageNamed:@"问号"];
    [contentView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom);
        make.left.equalTo(contentView.mas_left).offset(10);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(kBaseHeight);
    }];
    
    UILabel *line2 = [[UILabel alloc]init];
    line2.backgroundColor = [UIColor lightGrayColor];
    line2.alpha = 0.8;
    [contentView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(3);
        make.left.equalTo(contentView.mas_left).offset(40);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(line);
    }];
    

    CGFloat btnWidth = (contentView.width - 100*WIDTHSCALE6)/2;
    UIButton * surtBtn = [[UIButton alloc]init];
    [surtBtn setTitle:@"提交" forState:UIControlStateNormal];
    [surtBtn setTitleColor:BlueColor forState:UIControlStateNormal];
    surtBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [surtBtn addTarget:self action:@selector(surtBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
    surtBtn.layer.cornerRadius = 4;
    surtBtn.layer.masksToBounds = YES;
    surtBtn.layer.borderWidth = 1;
    surtBtn.layer.borderColor = BlueColor.CGColor;
    [contentView addSubview:surtBtn];
    [surtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(btnWidth);
        make.height.mas_offset(kBaseHeight);
        make.left.equalTo(contentView.mas_left).offset(40);
        make.bottom.equalTo(contentView.mas_bottom).offset(-10);
    }];
    
    UIButton * cancleBtn = [[UIButton alloc]init];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancleBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.layer.cornerRadius = 4;
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.borderWidth = 1;
    cancleBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    [contentView addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(btnWidth);
        make.height.mas_offset(kBaseHeight);
        make.right.equalTo(contentView.mas_right).offset(-40);
        make.bottom.equalTo(contentView.mas_bottom).offset(-10);
    }];
    
    if (type == 0) {//发布交易市场
        
        UIImageView *inputImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钱"]];
        [contentView addSubview:inputImageView];
        [inputImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(surtBtn.mas_top).offset(-3-5-2.5);
            make.left.equalTo(contentView.mas_left).offset(10);
            make.width.mas_offset(25);
            make.height.mas_offset(25);
        }];
        
        UITextField *inputTextfield = [[UITextField alloc]init];
        inputTextfield.placeholder = @"请填写报价";
        inputTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.inputTextfield = inputTextfield;
        [contentView addSubview:inputTextfield];
        [inputTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(surtBtn.mas_top).offset(-3);
            make.left.equalTo(inputImageView.mas_right).offset(5);
            make.right.equalTo(contentView.mas_right).offset(-10);
            make.height.mas_offset(kBaseHeight);
        }];
        
        UILabel *line3 = [[UILabel alloc]init];
        line3.backgroundColor = [UIColor lightGrayColor];
        line3.alpha = 0.8;
        [contentView addSubview:line3];
        [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(inputTextfield.mas_top).offset(-3);
            make.left.equalTo(contentView.mas_left).offset(40);
            make.right.equalTo(contentView.mas_right).offset(-10);
            make.height.mas_offset(line);
        }];
        
        
        DCChooseTableVIew *chooseTableView = [[DCChooseTableVIew alloc]initWithFrame:CGRectMake(25, 85, contentView.width-35, tableViewHeight)];
        chooseTableView.dataArr = dataSource;
        chooseTableView.tableView.scrollEnabled = dataSource.count<6?NO:YES;
        //选中内容
        chooseTableView.block = ^(NSString *chooseContent,NSMutableArray *chooseArr){
            weakself.chooseArray = chooseArr;
        };
        [contentView addSubview:chooseTableView];
        
    }else if (type == 1){//指定承运商
        SingleChooseCollectview *collecView = [SingleChooseCollectview ShareCollectviewWithFrame:CGRectMake(10, 85, contentView.width-20, tableViewHeight)];
        collecView.dataArr = dataSource;
        collecView.MyCollectView.scrollEnabled = dataSource.count >12?YES:NO;
        //选中内容
        collecView.block = ^(NSString * _Nonnull chooseContent, NSIndexPath * _Nonnull indexPath) {
            weakself.contentText = chooseContent;
        };
        [contentView addSubview:collecView];
        
    }else if (type == 2){//指派运力
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(10, 85, contentView.width-20, tableViewHeight)];
        [contentView addSubview:headerView];
        
        DCImageAndTextField *plateNumLabel = [[DCImageAndTextField alloc]init];
        plateNumLabel.frame = CGRectMake(10, 5, headerView.width - 50*WIDTHSCALE6-20, 40);
        plateNumLabel.imageView.image = IMAGENAME(@"车牌");
        plateNumLabel.textField.placeholder = @"请输入车牌";
        self.plateNumLabel = plateNumLabel;
        [headerView addSubview:plateNumLabel];
        
        UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBtn.backgroundColor = BlueColor;
        searchBtn.layer.cornerRadius = 4;
        searchBtn.clipsToBounds = YES;
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        searchBtn.titleLabel.font = FONT(15);
        [searchBtn addTarget:self action:@selector(searchBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:searchBtn];
        [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView).offset(6);
            make.left.equalTo(plateNumLabel.mas_right).offset(5);
            make.right.equalTo(headerView.mas_right).offset(-3);
            make.height.mas_offset(34);
        }];
        
        DCImageAndTextField *driverNameLabel = [[DCImageAndTextField alloc]init];
        driverNameLabel.frame = CGRectMake(plateNumLabel.x, plateNumLabel.bottom+5, plateNumLabel.width, plateNumLabel.height);
        driverNameLabel.imageView.image = IMAGENAME(@"用户头像");
        driverNameLabel.textField.placeholder = @"请输入司机姓名";
        self.driverNameLabel = driverNameLabel;
        [headerView addSubview:driverNameLabel];
    }
    
    /*
       color
     */
    contentView.backgroundColor = [UIColor whiteColor];
//    inputImageView.backgroundColor = [UIColor cyanColor];
//    titlelLabel.backgroundColor = [UIColor yellowColor];
//    textLabel.backgroundColor = [UIColor blueColor];
//    surtBtn.backgroundColor = [UIColor greenColor];
//    cancleBtn.backgroundColor = [UIColor greenColor];
//    inputTextfield.backgroundColor = [UIColor lightGrayColor];
    return backView;
}

-(void)surtBtnOnClicked:(UIButton *)btn
{
    UIView *view = (UIView *)btn.superview.superview;
    [view removeFromSuperview];
    if (self.type == 0) {
        if (self.sureBlock) {
            self.sureBlock(self.inputTextfield.text,self.chooseArray);
        }
    }else if (self.type == 1)
    {
        if (self.sureBlock) {
            self.sureBlock(self.contentText,nil);
        }
    }else if (self.type == 2){
        self.contentText = [NSString stringWithFormat:@"%@-%@",self.plateNumLabel.textField.text,self.driverNameLabel.textField.text];
        if (self.sureBlock) {
            self.sureBlock(self.contentText,nil);
        }
    }
}

-(void)cancleBtnOnClicked:(UIButton *)btn
{
    UIView *view = (UIView *)btn.superview.superview;
    [view removeFromSuperview];
    if (self.cancleBlock) {
        self.cancleBlock(self.cancleBlock);
    }
}

-(void)searchBtnDidClick
{
    DCLogFunc;
}

@end
